package com.lac.component.thread;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author xiaokuixiao
 * date2025/7/4 14:14
 */
public class ScheduledThreadPoolExample {
    public static void main(String[] args) {
        // 创建调度池，核心线程数为2
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);

        // 定时任务：5秒后开始，每隔3秒执行一次
        scheduler.scheduleAtFixedRate(() -> {
//            System.out.println("定时任务执行，时间：" + System.currentTimeMillis());
        }, 5, 3, TimeUnit.SECONDS);

        // 运行一段时间后关闭
        try {
            Thread.sleep(20000); // 20秒
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        scheduler.shutdown();
    }
}
